package com.open.party.cloud.view.home.dangYuanJiaoYu

import android.annotation.SuppressLint
import android.os.Bundle
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import com.sinothk.android.utils.inters.OnSuperListener
import com.bumptech.glide.Glide
import cn.android.x.model.data.ResultInfo
import cn.android.x.parent.AppEventBusRecycleViewTitleBaseActivity
import com.open.party.cloud.R
import com.open.party.cloud.ServerConfig
import com.open.party.cloud.model.YunKeBean
import com.open.party.cloud.model.YunKePlayBean
import com.open.party.cloud.model.YunKeVo
import com.open.party.cloud.view.base.VideoEntity
import com.open.party.cloud.view.home.dangYuanJiaoYu.adapter.LiangDuYunKeTangPlayListAdapter
import com.open.party.cloud.viewModel.BizViewModel
import com.sinothk.android.utils.XUtils
import com.sinothk.rxretrofit.bean.PageVo
import com.sinothk.widget.loadingRecyclerView.extend.LoadingRecycleViewHeader
import kotlinx.android.synthetic.main.comm_activity_load_recycle_view_load_tip.loadingRecyclerView
import kotlinx.android.synthetic.main.comm_activity_load_recycle_view_load_tip.loadingTipView
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode
import kotlin.collections.ArrayList

class YunKeTangClassListActivity : AppEventBusRecycleViewTitleBaseActivity<YunKeVo>(), OnSuperListener<YunKePlayBean> {

    private var yunKeInfo: YunKeBean? = null
    var viewModel: BizViewModel? = null
    private var adapter: LiangDuYunKeTangPlayListAdapter? = null

    companion object {
        var yunKePlayList: ArrayList<YunKePlayBean>? = null
    }

    override fun getLineDrawable(): Int {
        return R.drawable.list_divider
    }

    override fun getLayoutResId(): Int = R.layout.comm_activity_load_recycle_view_load_tip

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
//        window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
        setViewTitle("凉都云课堂")
        yunKeInfo = intent.getSerializableExtra("yunKeInfo") as YunKeBean

        viewModel = BizViewModel()

        initView()
    }

    private fun initView() {
        initRecycleLinearView(loadingRecyclerView)

        loadingRecyclerView.addHeaderView(createHeaderView())

        adapter = LiangDuYunKeTangPlayListAdapter(this)
        adapter!!.setOnSuperListener(this)
        loadingRecyclerView.adapter = adapter

        loadingRecyclerView.setPullRefreshEnabled(true)
        loadingRecyclerView.setLoadingMoreEnabled(false)

        loadingTipView.showLoading("努力加载中")

        loadingTipView.setRetryListener {
            loadingTipView.showLoading("努力加载中")
            refreshData()
        }
    }

    override fun loadData(pageVo: PageVo<YunKeVo>) {
        viewModel!!.getYunkePlayListData(yunKeInfo!!.id)
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public fun eventBusCallback(result: ResultInfo<ArrayList<YunKePlayBean>>) {
        if ("getYunkePlayListData" != result.eventType) {
            return
        }

        stopLoading(loadingRecyclerView, loadType)

        when (result.code) {
            ResultInfo.SUCCESS -> {
                if (result.data != null && result.data.size > 0) {
                    yunKePlayList = result.data
                    adapter!!.setData(yunKePlayList)
                    loadingTipView.showContent()
                } else {
                    loadingTipView.showEmpty("暂无数据", R.drawable.empty_nodata)
                    loadingRecyclerView.setLoadingMoreEnabled(false)
                }
            }
            ResultInfo.TOKEN_OVERDUE -> {
                loadingTipView.showError("Token过期，请重新登录")
            }
            else -> {
                loadingTipView.showError(result.msg)
            }
        }

        stopLoading(loadingRecyclerView, loadType)
    }

    override fun onClick(position: Int, itemData: YunKePlayBean, flag: String) {
        doPlay(itemData, position)
    }

    /**
     * 头部
     */
    private var yunKeLogoIv: ImageView? = null
    private var playBtn: TextView? = null
    private var jiFenTv: TextView? = null
    private var nameTv: TextView? = null
    private var contentTV: TextView? = null

    @SuppressLint("SetTextI18n")
    private fun createHeaderView(): View {
        val headerView: View = LoadingRecycleViewHeader.getViewByLayoutId(this, R.layout.liang_du_yun_ke_tang_play_list_header)
        yunKeLogoIv = headerView.findViewById(R.id.yunKeLogoIv)
        playBtn = headerView.findViewById(R.id.playBtn)
        nameTv = headerView.findViewById(R.id.nameTv)
        jiFenTv = headerView.findViewById(R.id.jiFenTv)
        contentTV = headerView.findViewById(R.id.contentTV)

        if (yunKeInfo != null && XUtils.string().isNotEmpty(yunKeInfo!!.imgUrl)) {
            Glide.with(this).load(XUtils.string().getNotNullValue(yunKeInfo!!.imgUrl))
                .placeholder(R.drawable.default_img).error(R.drawable.default_img)
                .into(yunKeLogoIv!!)
        }

        nameTv!!.text = XUtils.string().getNotNullValue(yunKeInfo!!.name)
        jiFenTv!!.text = "学习积分：${yunKeInfo!!.integral}"
        contentTV!!.text = XUtils.string().getNotNullValue(yunKeInfo!!.learningInfo)

        playBtn!!.setOnClickListener {
            doPlay(yunKePlayList!![0], 0)
        }
        return headerView
    }

    private fun doPlay(yunKePlayBean: YunKePlayBean?, position: Int) {
        if (yunKePlayBean == null) {
            XUtils.toast().show("数据不可用")
            return
        }

//        for (index in yunKePlayList!!.indices) {
//            yunKePlayList!![index].isPlaying = false
//        }
//        yunKePlayList!![position].isPlaying = true

        if (yunKePlayBean.type != null && yunKePlayBean.type.code != null && yunKePlayBean.type.code != "SP") {

            val titleStr = XUtils.string().getNotNullValue(yunKePlayBean.name)
            var url = ServerConfig.getFilePreviewUrl(XUtils.string().getNotNullValue(yunKePlayBean.fileUrl))

            if (XUtils.string().isNotEmpty(url)) {
                val bundle = Bundle()
                bundle.putString("titleTxt", titleStr)
                bundle.putString("url", url)

                bundle.putBoolean("isStart", yunKePlayBean.isStart)
                bundle.putBoolean("isFinish", yunKePlayBean.isFinish)
                bundle.putString("cmId", yunKePlayBean.id)
                bundle.putString("learnId", XUtils.string().getNotNullValue(yunKePlayBean.lmmId))

                YunKeTangStudyFileActivity.start(this, bundle)
            }

        } else {
            val videoEntity = VideoEntity()
            videoEntity.title = yunKePlayBean.name
            videoEntity.url = yunKePlayBean.fileUrl
            videoEntity.sid = yunKePlayBean.id

            XUtils.intent().openActivity(this@YunKeTangClassListActivity, YunKeTangStudyVideoActivity::class.java)
                .putSerializableExtra("DataSource", videoEntity)

                .putBooleanExtra("isStart", yunKePlayBean.isStart)
                .putBooleanExtra("isFinish", yunKePlayBean.isFinish)
                .putStringExtra("cmId", yunKePlayBean.id)
                .putStringExtra("learnId", XUtils.string().getNotNullValue(yunKePlayBean.lmmId))

                .start()
        }
    }

    override fun onResume() {
        super.onResume()
        refreshData()
    }
}